package view.equipe;

import java.awt.Color;
import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import rmi.RemoteObserver;
import rmi.UtilitairesRmi;
import server.models.equipe.IEquipe;
import server.models.tireur.ITireur;

public class EquipeView extends JPanel  implements RemoteObserver {

	private static final long serialVersionUID = 1L;
	private ArrayList<ITireur> listeTireurs;
	private IEquipe equipeObserver = null;
	private String nomClient = null;
	
	public EquipeView(IEquipe equipe, String nomClient) throws RemoteException, NotBoundException, AlreadyBoundException {
		super();
		this.nomClient = nomClient;
		this.equipeObserver = equipe;
		creerLabelEquipe(equipe, Color.BLACK);
		for (ITireur tireur : equipe.getTireurs()) {
			this.add(new TireurView(tireur));
		}
		this.listeTireurs = new ArrayList<ITireur>();
		try {
			equipe.addObserver((RemoteObserver)UtilitairesRmi.getInstance().obtenirEtBind("EquipeView" + "_" + equipe.getNom() + "_" + nomClient, this));			
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		this.repaint();
		this.setVisible(true);
	}

	public void update(Object observable, Object object) throws RemoteException {
		for (ITireur tireur : ((IEquipe)observable).getTireurs()) {
			if (!listeTireurs.contains(tireur)) {
				this.add(new TireurView(tireur));
				listeTireurs.add(tireur);
			}
		}
		
		this.validate();
	}

	private void creerLabelEquipe(IEquipe equipe, Color couleur) throws RemoteException {
		JLabel equipeLabel = new JLabel();
		equipeLabel.setText(equipe.getNom());
		equipeLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		equipeLabel.setForeground(couleur);
		equipeLabel.setHorizontalAlignment(SwingConstants.CENTER);
		equipeLabel.setVerticalAlignment(SwingConstants.CENTER);
		this.add(equipeLabel);
	}

}
